package com.qf.smsplatform.deadletter.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DeadLetterConfig {

    public static  final String POJO_EXCHANGE = "pojo.exchange";
    public static  final String POJO_QUEUE = "pojo.queue";
    public static  final String POJO_ROUTING_KEY = "#";

    public static  final String DEAD_EXCHANGE = "dead.exchange";
    public static  final String DEAD_QUEUE = "dead.queue";
    public static  final String DEAD_ROUTING_KEY = "#";

    public static  final String DEAD_KEY = "aaa.bbb.ccc";


    @Bean
    public Exchange pojoExchange(){
        return ExchangeBuilder.topicExchange(POJO_EXCHANGE).build();
    }

    @Bean
    public Queue pojoQueue(){
        Map<String, Object> args = new HashMap<>();
        // 设置普通队列的死信交换机
        args.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        // 设置普通队列的消息生存时间
//        args.put("x-message-ttl",5000);
        return QueueBuilder.durable(POJO_QUEUE).withArguments(args).build();
    }

    @Bean
    public Binding pojoBinding(Queue pojoQueue,Exchange pojoExchange){
        return BindingBuilder.bind(pojoQueue).to(pojoExchange).with(POJO_ROUTING_KEY).noargs();
    }


    @Bean
    public Exchange deadExchange(){
        return ExchangeBuilder.topicExchange(DEAD_EXCHANGE).build();
    }

    @Bean
    public Queue deadQueue(){
        return QueueBuilder.durable(DEAD_QUEUE).build();
    }

    @Bean
    public Binding deadBinding(Queue deadQueue,Exchange deadExchange){
        return BindingBuilder.bind(deadQueue).to(deadExchange).with(DEAD_ROUTING_KEY).noargs();
    }



}
